﻿/* 2013/6/10 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Cosmos.IO.FileSystemPath.Searcher
{
    /// <summary>
    /// Generates paths by specifying search condition.
    /// Searches file or directory by file system fuzzy search function.
    /// 
    /// Fuzzy search means every space in search pattern will be
    /// replaced by asterisk. And asterisks will be added at start
    /// and end of the pattern.
    /// </summary>
    public class FuzzyFilePathSearcher : SystemApiPathSearcher
    {
        /// <summary>
        /// The separators of search patterns.
        /// </summary>
        protected string[] separators = new string[] { " " };

        protected override string GenerateSearchPattern(string searchPattern)
        {
            if (String.IsNullOrEmpty(searchPattern))
            {
                return searchPattern;
            }

            string[] patterns = searchPattern.Split(separators,
                    StringSplitOptions.RemoveEmptyEntries);
            searchPattern = String.Join("*", patterns);
            searchPattern = String.Concat("*", searchPattern, "*");
            return searchPattern;
        }

        /// <summary>
        /// Gets or sets the separators of search patterns.
        /// </summary>
        public string[] Separators
        {
            get { return separators; }
            set { separators = value; }
        }
    }
}
